﻿#ifndef _C_RTSPPUSHER_H_INCLUDE_
#define _C_RTSPPUSHER_H_INCLUDE_

#include "rtsp_common.h"
#include "h264source.h"
#include "../../network/networkframemanager.h"

#include <QTcpSocket>

class CRtspPusher : public QTcpSocket
{
    Q_OBJECT

public:
    explicit CRtspPusher(QObject *parent = nullptr,
                         NetworkFrameManager *pNetworkFrameManager=NULL);
    ~CRtspPusher();

    /// 连接rtsp流媒体服务器
    bool openUrl(QString url);
    /// 断开服务器连接
    void Close();
    /// 检测和服务器是否连接成功
    bool IsConnected();

    /// 初始化h264
    void initH264(QSize screenSize);
    /// 卸载h264
    void clearnH264(void);

    /// 发送Packet数据
    qint64 sendPacketData(RtpPacket *pkt);
    /// 推送帧数据
    bool PushFrame(AVFrame *frame);
    /// 推送图片数据
    bool PushImage(QImage& image);

signals:
    void sockConnected(void);
    void sockDisConnect(void);
    /// 处理网络二进制消息
    void signalsNetBinary(QByteArray data);
    /// 日志消息
    void signalLog(QString log,QString type);
    /// 主备结束，可以发送数据
    void signalReadytofinish(void);

private slots:
    /// 处理接收数据
    void onReadyRead();
    /// 处理连接关闭
    void onDisconnected();
    /// 处理连接成功
    void onConnected();

private:
    /// 记录日志信息
    void Log(QString msg, QString type="info");

    /// 发送OPTIONS
    void SendOptions(void);
    /// 发送ANNOUNCE
    void SendAnnounce();
    /// 发送SETUP
    void SendSetup();
    /// 发送RECORD
    void SendRecord();

    void HandleRecord();
    bool HandleRtspRequest(QByteArray& data);

    int BuildOptionReq(const char* buf, int buf_size);
    int BuildAnnounceReq(const char* buf, int buf_size, const char *sdp);
    int BuildSetupTcpReq(const char* buf, int buf_size, int trackId);
    int BuildRecordReq(const char* buf, int buf_size);
    bool ParseResponse(QByteArray& data);
    std::string GetSdpMessage(void);
    void SetFrameType(uint8_t frame_type);
    void SetRtpHeader(RtpPacket *pkt);
    inline void SetClockRate(quint32 clock_rate) { m_clock_rate = clock_rate; }
    inline void SetPayloadType(quint32 payload) { m_RtpHeader.payload = payload; }

    /// 处理YUV240P数据
    bool HandleYuv240PFrame(unsigned char* yuvData);

    /// 发送数据
    qint64 send(QByteArray data);
    qint64 send2(const char *data,qint32 size);

private:
    NetworkFrameManager *m_NetworkFrameManager;                    /**< 网络处理框架 */
    RtspUrlInfo m_RtspUrlInfo;                                     /**< 用于保存Url配置信息 */
    Method m_Method;
    H264Source m_H264Source;
    bool m_isCloseWindows;                                         /**< 是否关闭窗口 */
    int m_Cseq;                                                    /**< cseq */
    bool m_is_setup;                                               /**< 是否设置成功 */
    bool m_is_play,m_has_key_frame;                                /**< 是否可以播放 */
    RtpHeader m_RtpHeader;
    quint16 m_packet_seq;
    std::string m_session;
    quint32 m_clock_rate;
    quint8 *mimageBuffer;                                          /**< 图片转换缓冲区 */
};

#endif // CRTSPPUSHER_H
